// This file is a part of Funky C++.
// Copyright (C) 2011, 2013  Valentin David
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "jack/client.hh"
#include "player_block.hh"
#include "signal/sound_source_block.hh"
#include "signal/loop_track.hh"
#include "signal/function.hh"
#include "signal/midi_source.hh"
#include "signal/loop_clock.hh"
#include "signal/rs.hh"
#include "signal/filter.hh"
#include <iostream>
#include <chrono>
#include <thread>

int main() {
  using namespace funky;

  jack::client jc{"The thing!"};

  auto midi = midi_source<float>(jc);
  auto control =
    loop_clock
    (rs(filter([](const jack::midi_event& e) {
	  std::bitset<3> ret;
	  ret.set(1);
	  if (e.data[0] == 0x90)
	    if (e.data[1] == 64) {
	      ret.set(0);
	      ret.set(2);
	    }
	  if (e.data[0] == 0x80)
	    if (e.data[1] == 64) {
	      ret.set(2);
	    }
	  return ret;
	}, midi),
      filter([](const jack::midi_event& e) {
	std::bitset<3> ret;
	if (e.data[0] == 0x80)
	  if (e.data[1] == 64)
	    ret.set(0);
	if (!(e.data[0] == 0x90) && !(e.data[0] == 0x80))
	  ret.set(2);
	else if (!(e.data[1] == 64))
	  ret.set(2);
	return ret;
	}, midi)));

  sound_source_block<512u> source{jc};
  auto l = loop_track(source, control);

  auto p = make_player_block<512u>(l, jc);
  std::this_thread::sleep_for(std::chrono::seconds(1));
  jc.activate();
  std::this_thread::sleep_for(std::chrono::seconds(100));

  return 0;
}
